package com.lagou;

import org.apache.catalina.LifecycleException;
import org.apache.catalina.startup.Tomcat;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;

import javax.servlet.ServletException;

/**
 * @description
 * @author: liuhongfei
 * @create: 2022/3/6 22:13
 */
public class SpringApplication {

    public static void run(Class<?> mainClass) {
        AnnotationConfigWebApplicationContext ac = new AnnotationConfigWebApplicationContext();
        ac.register(mainClass);
        ac.refresh();
        try {
            Tomcat tomcat = new Tomcat();
            tomcat.setPort(8080);
            tomcat.addWebapp("/", "D://soft");
            tomcat.start();

            //维持tomcat服务
            tomcat.getServer().await();
        } catch (LifecycleException e) {
            e.printStackTrace();
        } catch (ServletException e) {
            e.printStackTrace();
        }

    }
}
